package com.lukk.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

import javax.servlet.http.HttpSession;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.lukk.constant.Const;
import com.lukk.dao.ProvinceDAO;
import com.lukk.dao.WorkerDAO;
import com.lukk.pojo.InternalWorker;
import com.lukk.pojo.Province;
import com.lukk.pojo.Worker;
import com.lukk.pojo.WorkerSpace;
import com.lukk.service.IWorkerService;
import com.lukk.util.JsonUtil;

/**
 * 
 * WorkerServiceImpl 上午10:25:50
 * 
 * Copyright zhaocj Inc. All rights reserved.
 * Love ME Like Justin Bieber.
 */
@Service("workerService")
public class WorkerServiceImpl implements IWorkerService {
	private static Logger logger = LogManager.getLogger(WorkerServiceImpl.class.getName());
	@Autowired
	private WorkerDAO workerDAO;

	public WorkerDAO getWorkerDAO() {
		return workerDAO;
	}

	public void setWorkerDAO(WorkerDAO workerDAO) {
		this.workerDAO = workerDAO;
	}

	@Autowired
	private ProvinceDAO provinceDAO;

	public ProvinceDAO getProvinceDAO() {
		return provinceDAO;
	}

	public void setProvinceDAO(ProvinceDAO provinceDAO) {
		this.provinceDAO = provinceDAO;
	}

	@Override
	public Map<String, Object> selectByProvinceAndName(Integer offset, Integer limit, String sort, String order,
			String beginTime, String endTime, Integer status, String provinceCode, String workerName,
			String phoneNumber, Integer isDisable) {
		Map<String, Object> queryMap = new HashMap<String, Object>();
		queryMap.put("isDisable", isDisable);
		queryMap.put("sort", sort);
		queryMap.put("order", order);
		queryMap.put("beginTime", beginTime);
		queryMap.put("endTime", endTime);
		queryMap.put("status", status);
		queryMap.put("provinceCode", provinceCode);
		queryMap.put("workerName", workerName);
		queryMap.put("phoneNumber", phoneNumber);
		List<Worker> beans = workerDAO.selectByMap(queryMap);
		Map<String, Object> retsMap = new HashMap<String, Object>();
		int totalSize = beans.size();
		int toSize = (totalSize - offset) <= limit ? totalSize : offset + limit;
		retsMap.put("total", totalSize);
		retsMap.put("rows", beans.subList(offset, toSize));
		return retsMap;
	}

	@Override
	public boolean modifyByPrimaryKey(Worker worker) {
		System.out.println("id:"+worker.getWorkerId()+",CanGetIndent:"+worker.getCanGetIndent()+",HasGetIndent:"+worker.getHasGetIndent()+",NewIndentPush:"+worker.getNewIndentPush()+",OperateWallet:"+worker.getOperateWallet());
		workerDAO.modifyByPrimaryKey(worker);
		return true;
	}
	@Override
	public Map<String, Object> selectAllProvinceWorkersForInternalWorker(Integer offset, Integer limit, String sort,
			String order, String beginTime, String endTime, Integer status, String provinceName, String workerName,
			String phoneNumber, Integer isDisable, HttpSession session) {
		// 查询登陆员工负责的省份
		InternalWorker internalWorker = (InternalWorker) session.getAttribute(Const.SESSION_USER);
		List<Province> provincesForInternalWorker = provinceDAO.selectAvailableProvinces(internalWorker);
		// 或是空的 即是代表 没有负责审核的省份
		if (provincesForInternalWorker.isEmpty()) {
			Map<String, Object> retsMap = new HashMap<String, Object>();
			retsMap.put("total", 0);
			retsMap.put("rows", new ArrayList<>());
			return retsMap;
		} else {
			Map<String, Object> queryMap = new HashMap<String, Object>();
			logger.debug("一共有多少个省份：{}" + provincesForInternalWorker.size());
			queryMap.put("isDisable", isDisable);
			queryMap.put("sort", sort);
			queryMap.put("order", order);
			queryMap.put("beginTime", beginTime);
			queryMap.put("endTime", endTime);
			queryMap.put("status", status);
			queryMap.put("provinces", provincesForInternalWorker);
			queryMap.put("workerName", workerName);
			queryMap.put("phoneNumber", phoneNumber);
			List<Worker> beans = workerDAO.selectAllProvinceWorkersForInternalWorker(queryMap);
			Map<String, Object> retsMap = new HashMap<String, Object>();
			int totalSize = beans.size();
			int toSize = (totalSize - offset) <= limit ? totalSize : offset + limit;
			retsMap.put("total", totalSize);
			retsMap.put("rows", beans.subList(offset, toSize));
			return retsMap;
		}
	}

	@Override
	public Worker selectByPrimaryKey(Integer workerId) {
		Worker worker = workerDAO.selectByPrimaryKey(workerId);
		worker.setWorkSpaces(workerDAO.selectWorkerSpacesByWorkerID(workerId));
		return worker;
	}

	@Override
	public Map<String, Object> selectByProvinceAndStatus(Integer offset, Integer limit, String sort, String order,
			String provinceCode, String workerName, String typeValue, String phoneNumber, String queryType,
			HttpSession session) {
		Map<String, Object> retsMap = new HashMap<String, Object>();
		List<Province> provincesForInternalWorker = new ArrayList<Province>();

		// 查询登陆员工的全部省份来进行数据的筛选
		if ("0".equals(provinceCode)) {
			// 查询登陆员工负责的省份
			InternalWorker internalWorker = (InternalWorker) session.getAttribute(Const.SESSION_USER);
			provincesForInternalWorker = provinceDAO.selectAvailableProvinces(internalWorker);
			// 或是空的 即是代表 没有负责审核的省份
			if (provincesForInternalWorker.isEmpty()) {
				retsMap.put("total", 0);
				retsMap.put("rows", new ArrayList<>());
				return retsMap;
			} else {
				// 根据停用 未停用状态进行查询
				if ("disable".equals(queryType)) {
					Map<String, Object> queryMap = new HashMap<String, Object>();
					queryMap.put("isDisable", typeValue);
					queryMap.put("offset", offset);
					queryMap.put("status", 1);
					queryMap.put("limit", limit);
					queryMap.put("sort", sort);
					queryMap.put("order", order);
					queryMap.put("provinces", provincesForInternalWorker);
					queryMap.put("workerName", workerName);
					queryMap.put("phoneNumber", phoneNumber);
					retsMap.put("total", workerDAO.selecCountByProvinceAndStatus(queryMap));
					retsMap.put("rows", workerDAO.selectByProvinceAndStatus(queryMap));
					return retsMap;
				} else {
					// 根据员工的审核状态进行查询
					Map<String, Object> queryMap = new HashMap<String, Object>();
					queryMap.put("status", typeValue);
					queryMap.put("offset", offset);
					queryMap.put("limit", limit);
					queryMap.put("sort", sort);
					queryMap.put("order", order);
					queryMap.put("provinces", provincesForInternalWorker);
					queryMap.put("workerName", workerName);
					queryMap.put("phoneNumber", phoneNumber);
					retsMap.put("total", workerDAO.selecCountByProvinceAndStatus(queryMap));
					retsMap.put("rows", workerDAO.selectByProvinceAndStatus(queryMap));
					return retsMap;
				}
			}
		} else {
			// 根据停用 未停用状态进行查询
			if ("disable".equals(queryType)) {
				Province province = new Province();
				province.setAreaCode(provinceCode);
				provincesForInternalWorker.add(province);
				Map<String, Object> queryMap = new HashMap<String, Object>();
				queryMap.put("isDisable", typeValue);
				queryMap.put("offset", offset);
				queryMap.put("status", 1);
				queryMap.put("limit", limit);
				queryMap.put("sort", sort);
				queryMap.put("order", order);
				queryMap.put("provinces", provincesForInternalWorker);
				queryMap.put("workerName", workerName);
				queryMap.put("phoneNumber", phoneNumber);
				retsMap.put("total", workerDAO.selecCountByProvinceAndStatus(queryMap));
				retsMap.put("rows", workerDAO.selectByProvinceAndStatus(queryMap));
				return retsMap;
			} else {
				Province province = new Province();
				province.setAreaCode(provinceCode);
				provincesForInternalWorker.add(province);
				Map<String, Object> queryMap = new HashMap<String, Object>();
				queryMap.put("status", typeValue);
				queryMap.put("offset", offset);
				queryMap.put("limit", limit);
				queryMap.put("sort", sort);
				queryMap.put("order", order);
				queryMap.put("provinces", provincesForInternalWorker);
				queryMap.put("workerName", workerName);
				queryMap.put("phoneNumber", phoneNumber);
				retsMap.put("total", workerDAO.selecCountByProvinceAndStatus(queryMap));
				retsMap.put("rows", workerDAO.selectByProvinceAndStatus(queryMap));
				return retsMap;
			}

		}
	}

	@Override
	public Map<String, Object> selectWorkersByProvinceCode(Integer queryType, String provinceCode, Integer limit,
			Integer offset) {
		Map<String, Object> retsMap = new HashMap<String, Object>();
		Map<String, Object> queryMap = new HashMap<String, Object>();
		if (queryType == 2) {
			// 根据区域但会员工信息
			List<Integer> workerIDS = workerDAO.selectWorkerIDSByCountryCode(provinceCode);
			logger.debug("负责这块区域的员工：{}", workerIDS.size());
			List<Worker> workers = new ArrayList<Worker>();
			if (workerIDS.size() > 0) {
				workers = workerDAO.selectWorkersByCountryCode(workerIDS, offset, limit);
				for (Worker worker : workers) {
					List<WorkerSpace> workerSpaces = workerDAO.selectWorkerSpacesByWorkerID(worker.getWorkerId());
					worker.setWorkSpaces(workerSpaces);
				}
			}
			retsMap.put("rows", workers);
			retsMap.put("total", workerIDS.size());
			return retsMap;
		}
		queryMap.put("limit", limit);
		queryMap.put("offset", offset);
		queryMap.put("queryCode", provinceCode);
		queryMap.put("queryType", queryType);
		List<Worker> workers = workerDAO.selectWorkersByProvinceCode(queryMap);
		for (Worker worker : workers) {
			List<WorkerSpace> workerSpaces = workerDAO.selectWorkerSpacesByWorkerID(worker.getWorkerId());
			worker.setWorkSpaces(workerSpaces);
		}
		retsMap.put("rows", workers);
		retsMap.put("total", workerDAO.selectWorkersCountByProvinceCode(queryMap));
		return retsMap;
	}

	@Override
	public Map<String, Object> selectWorkerWithDrawalInfo(Integer offset, Integer limit, String beginTime,
			String endTime, Integer withdrawalStatus, String provinceCode, String workerName, String phoneNumber,
			HttpSession session) {
		logger.debug("当前查询的提现状态：{}", withdrawalStatus);
		Map<String, Object> retsMap = new HashMap<String, Object>();
		List<Province> provincesForInternalWorker = new ArrayList<Province>();
		// 查询该员工所有负责省份的信息
		if ("0".equals(provinceCode)) {
			// 查询登陆员工负责的省份
			InternalWorker internalWorker = (InternalWorker) session.getAttribute(Const.SESSION_USER);
			provincesForInternalWorker = provinceDAO.selectAvailableProvinces(internalWorker);
			// 或是空的 即是代表 没有负责审核的省份
			if (provincesForInternalWorker.isEmpty()) {
				retsMap.put("total", 0);
				retsMap.put("rows", new ArrayList<>());
				return retsMap;
			} else {
				Map<String, Object> queryMap = new HashMap<String, Object>();
				queryMap.put("offset", offset);
				queryMap.put("withdrawalStatus", withdrawalStatus);
				queryMap.put("limit", limit);
				queryMap.put("provinces", provincesForInternalWorker);
				queryMap.put("workerName", workerName);
				queryMap.put("phoneNumber", phoneNumber);
				retsMap.put("total", workerDAO.selectWorkerCountWithDrawalInfo(queryMap));
				retsMap.put("rows", workerDAO.selectWorkerWithDrawalInfo(queryMap));
				return retsMap;
			}
		} else {
			Province province = new Province();
			province.setAreaCode(provinceCode);
			provincesForInternalWorker.add(province);
			Map<String, Object> queryMap = new HashMap<String, Object>();
			queryMap.put("offset", offset);
			queryMap.put("withdrawalStatus", withdrawalStatus);
			queryMap.put("limit", limit);
			queryMap.put("provinces", provincesForInternalWorker);
			queryMap.put("workerName", workerName);
			queryMap.put("phoneNumber", phoneNumber);
			retsMap.put("total", workerDAO.selectWorkerCountWithDrawalInfo(queryMap));
			retsMap.put("rows", workerDAO.selectWorkerWithDrawalInfo(queryMap));
			return retsMap;
		}
	}

	@Override
	public List<Integer> selectAllocatedReduce(String workerID) {
		List<Integer> retList = new ArrayList<Integer>();
		Map<String, Integer> retMaps = workerDAO.selectAllocatedReduce(workerID);
		logger.debug("工人师傅拥有的权限：{}", JsonUtil.toJson(retMaps));
		for (Map.Entry<String, Integer> entry : retMaps.entrySet()) {
			System.out.println("key= " + entry.getKey() + " and value= " + entry.getValue());
			/*if (entry.getKey().equals("canGetIndent")) {
				System.out.println(entry.getKey()+",===="+entry.getValue());
				//retList.set(0, entry.getValue());
				
			}
			if (entry.getKey().equals("newIndentPush")) {
				System.out.println(entry.getKey()+",===="+entry.getValue());
				//retList.set(1, entry.getValue());
			}
			if (entry.getKey().equals("hasGetIndent")) {
				System.out.println(entry.getKey()+",===="+entry.getValue());
				//retList.set(2, entry.getValue());
			}
			if (entry.getKey().equals("orperateWallet")) {
				System.out.println(entry.getKey()+",===="+entry.getValue());
				//retList.set(3, entry.getValue());
			}		*/	
		}
		
		retList.add(0,retMaps.get("canGetIndent"));
		retList.add(1,retMaps.get("newIndentPush"));
		retList.add(2,retMaps.get("hasGetIndent"));
		retList.add(3,retMaps.get("operateWallet"));
		System.out.println(retList.toString()+"==========tostring");
		/*Set<String> keySet = retMaps.keySet();
		int i = 0;
		for (String string : keySet) {
			if (retMaps.get(string) == 1) {
				retList.add(i);
			}
			i++;
		}*/
		return retList;
	}

	

}